package Q39;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合总数
 * @author 23737
 * @time 2021.12.7
 */
public class Test {
    public static void main(String[] args) {

    }
}

class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        if(candidates.length == 0){
            return res;
        }
        List<Integer> cam = new ArrayList<Integer>();
        dfs(candidates,target,res,cam,0);
        return res;
    }
    public void dfs(int[] candidates,int target,List<List<Integer>> res,List<Integer> cam,int index){
        if(index == candidates.length){
            return;
        }
        if(target == 0){
            res.add(new ArrayList<Integer>(cam));
            return;
        }
        dfs(candidates,target,res,cam,index+1);
        if(candidates[index] <= target){
            cam.add(candidates[index]);
            dfs(candidates,target-candidates[index],res,cam,index);
            cam.remove(cam.size() - 1);
        }
    }
}